package com.gbx.ximalayating.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.gbx.ximalayating.R;
import com.gbx.ximalayating.adapter.AnchorViewPagerAdapter;
import com.gbx.ximalayating.entity.AnchorJsonEntity;
import com.gbx.ximalayating.entity.ListData;
import com.gbx.ximalayating.utils.MyHttpUtils;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class AnchorFragment extends Fragment implements Callback<AnchorJsonEntity> {
    private List<ListData> list;
    private RecyclerView recyclerView;
    private AnchorViewPagerAdapter adapter;
    private int page;

    public AnchorFragment() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_anchor, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler);
        list = new ArrayList<>();
        adapter = new AnchorViewPagerAdapter(list, getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (recyclerView.canScrollVertically(1) == false) {
                    if (page == 4) {
                        Toast.makeText(getContext(), "没有更多主播了", Toast.LENGTH_SHORT).show();
                    } else {
                        addPage();
                    }
                }
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }
        });
        addPage();
    }

    public Call<AnchorJsonEntity> getAnchorJsonEntityCall(int page) {
        Call<AnchorJsonEntity> android = MyHttpUtils.getService("http://mobile.ximalaya.com/")
                .getAnchorJsonEntityCall("android", page);
        return android;
    }

    private void addPage() {
        getAnchorJsonEntityCall(page++).enqueue(this);
    }

    @Override
    public void onResponse(Call<AnchorJsonEntity> call, Response<AnchorJsonEntity> response) {
        if (response.body().getMsg().equals("成功")) {
            adapter.addData(response.body().getList());
        }
    }

    @Override
    public void onFailure(Call<AnchorJsonEntity> call, Throwable t) {
        t.printStackTrace();
        Toast.makeText(getContext(), "网络异常，请检查网络！", Toast.LENGTH_SHORT).show();
    }
}
